package lumis.service.wsrp.producer;

import javax.persistence.NoResultException;

import lumis.doui.source.IDataProvider;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularData;
import lumis.doui.source.TabularSource;
import lumis.doui.source.field.ITabularSourceField;
import lumis.portal.PortalException;
import lumis.portal.UnexpectedException;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.serviceinterface.ServiceInterfaceConfig;
import lumis.portal.serviceinterfaceinstance.ServiceInterfaceInstanceConfig;
import lumis.util.IResource;
import lumis.util.ITransaction;

import com.sun.portal.wsrp.producer.ProducerException;

/**
 * {@link IDataProvider} that reads the current service instance's
 * {@link ProducerImpl} and if it exists populate a {@link TabularData} with the
 * right values for the interface.
 * 
 * @version $Revision: 11180 $ $Date: 2009-12-04 18:13:02 -0200 (Fri, 04 Dec 2009) $
 * @since 5.5.0
 */
public class ProducerAdministrationListDataProvider implements IDataProvider<TabularSource<ITabularSourceField>>
{
	public void loadData(SessionConfig sessionConfig, TabularSource<ITabularSourceField> source, ITransaction transaction) throws PortalException
	{
		String serviceInstanceId = source.getDouiContext().getRequest().getServiceInstanceConfig().getId();
		// get the producer associated with the current service instance
		ProducerImpl producer;
		try
		{
			producer = (ProducerImpl) ManagerFactory.getEntityManager().createNamedQuery(ProducerImpl.NAMED_QUERY_FIND_PRODUCER_BY_NAMESPACE).setParameter("namespace", serviceInstanceId)
					.getSingleResult();
		}
		catch (NoResultException e)
		{
			producer = null;
		}

		// if there is a producer associated with the current service instance
		// we should populate a list with its offeredPortletNames
		if (producer != null)
		{
			try
			{
				for (String offeredPortlet : producer.getOfferedPortletNames())
				{
					ISourceData row = source.getData().addRow();
					row.put("serviceInterfaceInstanceId", offeredPortlet);
					ServiceInterfaceInstanceConfig serviceInterfaceInstanceConfig = ManagerFactory.getServiceInterfaceInstanceManager().get(sessionConfig, offeredPortlet, transaction);
					ServiceInterfaceConfig serviceInterfaceConfig = ManagerFactory.getServiceInterfaceManager().getServiceInterfaceConfig(
							serviceInterfaceInstanceConfig.getServiceInterfaceId(), transaction);
					String displayName = ManagerFactory.getServiceInterfaceInstanceManager().getDisplayName(serviceInterfaceInstanceConfig,
							transaction);
					row.put("displayName", localize(displayName, serviceInterfaceConfig.getResource()));
					String pageId = serviceInterfaceInstanceConfig.getPageId();
					String pagePath = ManagerFactory.getPageManager().getPath(sessionConfig, pageId, transaction);
					row.put("pagePath", pagePath);
					row.put("interfaceId", serviceInterfaceConfig.getId());
				}
			}
			catch (ProducerException e)
			{
				throw new UnexpectedException(e);
			}

			// apply post load processing
			source.applyPostLoadFilters();
			source.getData().sort(source);
			source.applyPostLoadPagination();
		}
	}

	/**
	 * Localize a String with the given {@link IResource}.
	 * 
	 * @param displayName
	 *            the string to localize.
	 * @param resource
	 *            the resource to base the localization on.
	 * @return the localized string.
	 * @since 5.5.0
	 */
	private String localize(String displayName, IResource resource) throws PortalException
	{
		return ManagerFactory.getLocalizationManager().localize(displayName, resource);
	}
}
